package com.example.openguass.controller;


import com.example.openguass.entity.CommonResult;
import com.example.openguass.entity.Dish;
import com.example.openguass.entity.OrderDetail;
import com.example.openguass.service.OrderDetailService;
import com.example.openguass.validation.Update;
import com.example.openguass.vo.DishPageVO;
import com.example.openguass.vo.DishVO;
import com.example.openguass.vo.OrderDetailPageVO;
import com.example.openguass.vo.OrderDetailVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@RestController
@CrossOrigin
@RequestMapping("/order-detail")
@Api(value = "OrderDetailController", tags = "订单详情接口")
public class OrderDetailController {
    private OrderDetailService orderDetailService;
    @Autowired
    private void setOrderDetailService(OrderDetailService orderDetailService){
        this.orderDetailService = orderDetailService;
    }

    @PostMapping("insert")
    public CommonResult insert(@RequestBody OrderDetailVO orderDetailVO){
        CommonResult result = new CommonResult().init();
        if (orderDetailService.insert(orderDetailVO)){
            return result.success("创建订单详情成功",orderDetailVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"创建订单详情失败");
        }
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody @Validated({Update.class}) OrderDetailVO orderDetailVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            return (CommonResult) result.failIllegalArgument(bindingResult.getFieldErrors()).end();
        }
        if (orderDetailService.getById(orderDetailVO.getId()) == null){
            return (CommonResult) result.failCustom(-10086,"不存在当前订单详情");
        }
        if (orderDetailService.update(orderDetailVO)){
            return result.success("orderDetail",orderDetailVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"更新订单详情失败");
        }
    }

    @GetMapping("/delete/{orderDetailId}")
    public CommonResult delete(@PathVariable String orderDetailId){
        CommonResult result = new CommonResult().init();
        if (orderDetailService.getById(orderDetailId) == null){
            return (CommonResult) result.failCustom(-10086,"该订单详情不存在");
        }
        if (orderDetailService.removeById(orderDetailId)){
            return (CommonResult) result.success();
        }else {
            return (CommonResult) result.failCustom(-10086,"删除订单详情失败");
        }
    }

    @GetMapping("/select/{orderDetailId}")
    public CommonResult select(@PathVariable String orderDetailId){
        CommonResult result = new CommonResult().init();
        if (orderDetailService.getById(orderDetailId) == null){
            return (CommonResult) result.failCustom(-10086,"该订单详情不存在");
        }
        OrderDetail orderDetail = orderDetailService.getById(orderDetailId);
        return result.success("orderDetail",orderDetail);
    }


    @PostMapping("findPage")
    public CommonResult findPage(@RequestBody OrderDetailPageVO orderDetailPageVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            result.failIllegalArgument(bindingResult.getFieldErrors()).end();
            return result;
        }
        return (CommonResult) result.success("page",orderDetailService.findPage(orderDetailPageVO)).end();
    }

    @GetMapping("/topDish")
    public CommonResult topDish(){
        CommonResult result = new CommonResult().init();
        result.success("data",orderDetailService.getTopDish());
        return result;
    }
}
